//
//  CallLimit.m
//  gdemm
//
//  Created by 李园园 on 2017/4/14.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "CallLimit.h"
#import <CallKit/CallKit.h>

@interface CallLimit ()<CXCallObserverDelegate>
@property (nonatomic,assign) BOOL mIsBlackMode;
@property(nonatomic,assign)BOOL isInable;
@end

@implementation CallLimit

static CallLimit * _callLimit;

-(instancetype)init{
    if (self = [super init]) {
        _mIsBlackMode = false;
        _isInable = NO;
    }
    return self;
}

+(instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _callLimit = [[self alloc]init];
    });
    return _callLimit;
}

-(NSMutableArray *)mPhoneNumberList{
    if (_mPhoneNumberList == nil) {
        _mPhoneNumberList = [NSMutableArray array];
    }
    return _mPhoneNumberList;
}

- (NSString *)supportPolicy{
    return CALL_LIMIT;
}

-(BOOL)startPolicyWithPolicyType:(NSString *)policyType policyContent:(NSDictionary *)policyContent{
    self.isInable = YES;
    _mIsBlackMode =  [policyContent[@"mode"] isEqualToString:@"black"]? true:false;
    NSArray * array  = policyContent[@"phoneNumber"];
    [self.mPhoneNumberList  removeAllObjects];
    [array enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *phonrNum = [NSString stringWithFormat:@"86%@",obj[@"phone"]] ;
        [self.mPhoneNumberList addObject:phonrNum];
    }];
    NSUserDefaults * userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.cn.neiwang.gview"];
    NSArray *sortedArray = [self.mPhoneNumberList sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    [userDefaults setObject:sortedArray forKey:@"blackPhoneNum"];
    [userDefaults synchronize];
    [self checkPermissions];
    [self updateData];
    return true;
}
-(BOOL)stopPolicyWithPolicyType:(NSString *)policyType{
    self.isInable = NO;
      NSUserDefaults * userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.cn.neiwang.gview"];
    [userDefaults setObject:nil forKey:@"blackPhoneNum"];
    [userDefaults synchronize];
    [self updateData];
    return false;
}

-(BOOL)valid{
    return self.isInable;
}
-(void)run{
    [self checkPermissions];
}

-(void)checkPermissions
{
    CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    // 获取权限状态
    [manager getEnabledStatusForExtensionWithIdentifier:@"cn.neiwang.emm.CallKit" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
        if (!error) {
            NSString *title = nil;
            if ((enabledStatus == CXCallDirectoryEnabledStatusDisabled )||(enabledStatus == CXCallDirectoryEnabledStatusUnknown)) {
                title = @"未授权，请在设置->电话->来电阻止与身份识别内授权相关权限";
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIWindow *window = [UIApplication sharedApplication].keyWindow;
                    [window makeToast:title];
                });
            }
        }
    }];
}
-(void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call{
}
-(void)updateData
{
    CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    [manager reloadExtensionWithIdentifier:@"cn.neiwang.emm.CallKit" completionHandler:^(NSError * _Nullable error) {
    }];

}
@end
